package lab_14.com.acme.bankapp.client;

import java.io.*;
import java.net.*;


public class BankClient {

	Socket requestSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	String message;

	void run() {
		try {
			// 1. creating a socket to connect to the server
			requestSocket = new Socket("localhost", 2004);
			System.out.println("Connected to localhost in port 2004");
			// 2. get Input and Output streams
			out = new ObjectOutputStream(requestSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(requestSocket.getInputStream());
			// 3: Communicating with the server
			do {
				try {
					negotiate();
				} catch (ClassNotFoundException e) {
					System.err.println("data received in unknown format");
				}
			} while (!message.equals("Bye!"));
		} catch (UnknownHostException e) {
			System.err.println("You are trying to connect to an unknown host!");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 4: Closing connection
			try {
				in.close();
				out.close();
				requestSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void negotiate() throws IOException, ClassNotFoundException {
		String command;
		while (true) {
			command = readCommand();
			sendMessage(command);
			message = (String) in.readObject();
			System.out.println("server>" + message);
		}
	}
	
	/**
	 * The mock method just to emulate negotiate() method for testing
	 * 
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unused")
	private void negotiate_mock() throws IOException, ClassNotFoundException {
		message = (String) in.readObject();
		System.out.println("server>" + message);

		sendMessage("add");
		message = (String) in.readObject();
		System.out.println("server>" + message);
		
		sendMessage("accounttype=c;balance=77.0;overdraftlimit=10.0;name=Maxim;gender=m;");
		message = (String) in.readObject();
		System.out.println("server>" + message);
		
		sendMessage("printbank");
		message = (String) in.readObject();
		System.out.println("server>" + message);
		
		sendMessage("exit");
		message = (String) in.readObject();
		System.out.println("server>" + message);
	}
	
	private String readCommand() {
		StringBuilder sb = new StringBuilder();
		InputStreamReader input = new InputStreamReader(System.in);
		Character symbol = 0;
		while(true) {
			try {
				symbol = (char) input.read();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (symbol.equals('\n')) break;
			sb.append(symbol);
		}
		return sb.toString().trim();
	}

	void sendMessage(final String msg) {
		try {
			out.writeObject(msg);
			out.flush();
			System.out.println("client>" + msg);
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}

	public static void main(final String args[]) {
		BankClient client = new BankClient();
		client.run();
	}

}
